package com.example.mycalculate;

import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btnClear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((TextView) findViewById(R.id.txtMsg)).setText("");
            }
        });
        findViewById(R.id.btnDelete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = ((TextView) findViewById(R.id.txtMsg)).getText().toString();
                if (text.length() > 0) {
                    ((TextView) findViewById(R.id.txtMsg)).setText(text.substring(0, text.length() - 1));
                }
            }
        });
        //计算结果
        findViewById(R.id.btnCalc).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //逆波兰表达式求值
                String expStr = ((TextView) findViewById(R.id.txtMsg)).getText().toString();
                String[] exp = new String[expStr.length()];
                for (int i = 0; i < expStr.length(); i++) {
                    exp[i] = expStr.charAt(i) + "";
                }
                int result = evalRPN(exp);
                TextView txtMsg = findViewById(R.id.txtMsg);
                txtMsg.setText(txtMsg.getText().toString().concat("=" + result + ""));

            }
        });
    }

    public void myClick(View view) {
        Button btn = (Button) view;
        TextView txtMsg = findViewById(R.id.txtMsg);
        CharSequence btn0Txt = btn.getText();
        txtMsg.setText(txtMsg.getText().toString().concat(btn0Txt.toString()));

    }

    public void execCalc(View view) {
        Button btn = (Button) view;
        TextView txtMsg = findViewById(R.id.txtMsg);
        CharSequence btn0Txt = btn.getText();
        txtMsg.setText(txtMsg.getText().toString().concat(btn0Txt.toString()));
    }

    public int evalRPN(String[] tokens) {
        Stack stack = new Stack();
        for (String s : tokens) {
            if (isNumber(s)) {
                stack.push(s);
            } else {
                Integer num1 = Integer.parseInt(stack.pop() + "");
                Integer num2 = Integer.parseInt(stack.pop() + "");
                Integer t = mathMath(num2, num1, s);
                stack.push(t);
            }
        }
        return Integer.parseInt(stack.pop() + "");
    }

    //执行数学运算
    public static Integer mathMath(Integer num1, Integer num2, String operator) {
        if ("+".equals(operator))
            return Math.addExact(num1, num2);
        if ("-".equals(operator))
            return Math.subtractExact(num1, num2);
        if ("*".equals(operator))
            return Math.multiplyExact(num1, num2);
        if ("/".equals(operator))
            return num1 / num2;
        return 0;
    }

    //判断是否是数字,如果是数字则返回true,否则返回false
    public static boolean isNumber(String str) {
        if (str == null || str.length() == 0)
            return false;
        if (str.length() == 1) {
            if (!(str.charAt(0) >= '0' && str.charAt(0) <= '9')) {
                return false;
            }
        }
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(0) == '+' || str.charAt(0) == '-') {
                continue;
            }
            if (!(str.charAt(i) >= '0' && str.charAt(i) <= '9')) {
                return false;
            }
        }
        return true;
    }

    //计算两个符号的优先级 返回小等于零的数表示后面优先级高 否则前面优先级高
    public static int maxOperator(String operator1, String operator2) {
        int weight1 = getPriority(operator1);//符号一权重
        int weight2 = getPriority(operator2);//符号二权重
        return weight1 - weight2;
    }

    //获取符号权重
    public static int getPriority(String operator) {
        if ("+".equals(operator) || "-".equals(operator))
            return 1;
        else if ("*".equals(operator) || "/".equals(operator) || "%".equals(operator))
            return 2;
        else
            return 0;
    }

}